//向文件添加单词
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 40

int main()
{
    FILE *fp;
    char words[MAX];

    if ((fp = fopen("words", "a+")) == NULL)
    {
        fprintf(stdout, "can't open file.\n");
        exit(1);
    }
    puts("Please input words:\n");

    while (gets(words) != NULL && words[0] != '\0')
        fprintf(fp, "%s ", words); //向文件写入文本内容

    puts("File Contents:\n");

    rewind(fp); //回到文件的开始处

    while (fscanf(fp, "%s ", words) == 1) //从文件读取文本内容
        puts(words);
    if (fclose(fp) != 0)
        fprintf(stderr, "error closing!\n");

    return 0;
}
